<script setup lang="ts">
import { onMounted } from 'vue'
import { getDiskIOStats } from '@/api/disk'
import DiskIOStatItem from './DiskIOStatItem.vue'
import { useDataFetching } from '@/utils/dataFetching'

const {
  data: diskIOStats,
  loading,
  error,
  fetchData,
} = useDataFetching(getDiskIOStats, '获取磁盘IO统计信息失败')

onMounted(() => {
  fetchData()
})
</script>

<template>
  <div class="disk-io-stats-container">
    <div class="disk-io-stats-header">磁盘IO统计</div>
    <div v-if="loading" class="loading">
      <div class="loading-spinner"></div>
      <p>正在加载磁盘IO统计...</p>
    </div>
    <div v-else-if="error" class="error">
      <p>{{ error }}</p>
    </div>
    <div v-else-if="diskIOStats && diskIOStats.length > 0" class="disk-stats-grid">
      <DiskIOStatItem v-for="diskIO in diskIOStats" :key="diskIO.name" :diskIOStat="diskIO" />
    </div>
    <div v-else class="no-data">
      <p>暂无磁盘IO统计数据</p>
    </div>
  </div>
</template>

<style scoped lang="scss">
.disk-io-stats-container {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 12px;
  padding: 2rem;
  backdrop-filter: blur(10px);
}

.disk-io-stats-header {
  font-size: 1.5rem;
  font-weight: 600;
  color: #ffffff;
  margin-bottom: 2rem;
  text-align: center;
}

.disk-stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
  gap: 1.5rem;
}

.loading,
.error,
.no-data {
  text-align: center;
  padding: 3rem 1rem;
  color: #9ca3af;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid rgba(59, 130, 246, 0.3);
  border-top: 3px solid #3b82f6;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 0 auto 1rem;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.error {
  color: #ef4444;
}

@media (max-width: 768px) {
  .disk-stats-grid {
    grid-template-columns: 1fr;
    gap: 1rem;
  }

  .disk-io-stats-container {
    padding: 1rem;
  }
}
</style>
